<?php
namespace app\admin\model;

use think\Model;

class Category extends Model
{
    protected $table = 'category';
    protected $autoWriteTimestamp = 'datetime';

    public function image()
    {
        return $this->hasOne('Image','id','topic_img_id');

    }

    public static function add($param =[])
    {
        $image = Image::create(['url'=>$param['url'],'from'=>1]);

        $cate = Category::create(['name'=>$param['name'],'topic_img_id'=>$image->getLastInsID(),'description'=>$param['description']]);

        if ($cate){

            return true;
        }

        return false;

    }

    public static function cate_del($ids)
    {
        if (!is_array($ids)) {

            $ids = explode(' ', $ids);
        }

        foreach ($ids as $k => $v) {

            $cate = Category::get($v);

            if (!$cate) return false;

            Image::destroy($cate->topic_img_id);

            $cate->delete();
        }

        return true;
    }

}